# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import akshare as ak
import datetime
import time
import numpy as np

import mysqlUtils


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
    print(ak.__version__)
    stock_cyq_em_df = ak.stock_cyq_em(symbol="002283", adjust="")
    print(stock_cyq_em_df)

    # print(stock_cyq_em_df.columns)
    # print(stock_cyq_em_df.values)
    my_list = stock_cyq_em_df.values
    for element in my_list:
        # print(element)
        for ele in element:
            print(ele, end=' ')
        print()

    my_list = stock_cyq_em_df.columns
    for element in my_list:
        print(element, end=' ')


    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="002283", period="daily",
                                            start_date="20240401", end_date='20240510',
                                            adjust="")
    print()
    my_list = stock_zh_a_hist_df.columns
    for element in my_list:
        print(element, end=' ')
    print()
    print(stock_zh_a_hist_df)

    #stock_individual_info_em_df = ak.stock_individual_info_em(symbol="002283")
    #print(stock_individual_info_em_df)

    #stock_bid_ask_em_df = ak.stock_bid_ask_em(symbol="002283")
    #print(stock_bid_ask_em_df)
    # print( type(stock_cyq_em_df) )

def get_cmjzdHisData( data, gpdm, gpmc ):
    # print(data)
    sumdata = []
    sumdate = []
    itemdata = []
    itemdate = []
    for element in data:
        # print(element)
        itemdata = [gpdm, gpmc, element[0].strftime('%Y-%m-%d'),
                   round(element[1]*100, 2), element[2],
                   element[3],element[4],  round(element[5]*100, 2),
                   element[6], element[7], round(element[8]*100, 2), ]
        itemdate = [element[0].strftime('%Y-%m-%d'), gpdm]
        # sumdata.append( temdata )
        # sumdate.append( [element[0].strftime('%Y-%m-%d'), gpdm] )
        # break

    sumdata.append( itemdata )
    sumdate.append( itemdate )
    # print(sumdata)
    # print(sumdate)
    # msq = mysqlUtils.MySqlOp()
    # msq.dataCmjzdHisInsertDb(sumdata, sumdate)

def get_cmdata(gpcode):
    '''
    [['股票代码' '002283']
     ['股票简称' '天润工业']
     ['总股本' 1139457178.0]
     ['流通股' 1003824929.0]
     ['总市值' 5822626179.58]
     ['流通市值' 5129545387.190001]
     ['行业' '汽车零部件']
     ['上市时间' 20090821]]
    '''
    #股票汇总
    # gpcode = "002283"
    stockhz = ak.stock_individual_info_em(symbol=gpcode)
    gptitle = stockhz.values[1][1]
    # print(stockhz)
    dataList = [stockhz.values[0][1],stockhz.values[1][1],
                round(stockhz.values[2][1] / 100000000, 0),
                round(stockhz.values[3][1] / 100000000, 0),
                round(stockhz.values[4][1] / 100000000, 0),
                round(stockhz.values[5][1] / 100000000, 0),
                stockhz.values[6][1],stockhz.values[7][1], ]

    stock_cm = ak.stock_cyq_em(symbol=gpcode, adjust="")
    #print(stock_cyq_em_df)
    # print(stock_cm.columns)
    # print(stock_cm.values[89])
    get_cmjzdHisData(stock_cm.values, gpcode, gptitle)

    dataList.append(stock_cm.values[89][0].strftime('%Y-%m-%d'))
    dataList.append(round(stock_cm.values[89][1]*100, 0))
    dataList.append( stock_cm.values[89][2] )

    dataList.append(stock_cm.values[89][3])
    dataList.append(stock_cm.values[89][4])
    dataList.append( round(stock_cm.values[89][5]*100, 0) )
    dataList.append(stock_cm.values[89][6])
    dataList.append(stock_cm.values[89][7])
    dataList.append(round(stock_cm.values[89][8]*100, 0))
    # print(dataList)

    hisdata = get_hisdata(gpcode,gptitle)
    msq = mysqlUtils.MySqlOp()
    msq.dataCmfInsertDb(dataList,hisdata)


def get_spotDataInsertDb():
    data = get_spotdata()
    gpdmdata = []
    if (len(data) <= 0):
        print("获取线上数据失败")
        return gpdmdata
    msq = mysqlUtils.MySqlOp()
    msq.dataSpotInsertDb(data)
    for element in data:
        gpdmdata.append( element[0] )
    return gpdmdata;



def get_zdfd(data):
    print('============================================')
    max=0
    max_date=""
    min=3000
    min_date=""
    for element in data:
        if(max<element[2]):
            max=element[2]
            max_date=element[0].strftime('%Y%m%d')
        if (min > element[2]):
            min = element[2]
            min_date = element[0].strftime('%Y%m%d')
    print( max , min, max_date, min_date )
    if( max_date > min_date):
        max = round( (max-min)*100/min, 2 )
    else:
        max = round( (min-max)*100/min, 2 )
    print(max)
    return max



def get_hisdata( gpcode,gptitle ):
    current_date = datetime.date.today()
    sdate = current_date.strftime("%Y%m%d")
    # 计算60天前的日期
    ten_days_ago = current_date - datetime.timedelta(days=60)
    # 打印30天前的日期
    edate = ten_days_ago.strftime("%Y%m%d")
    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol=gpcode, period="daily", start_date=edate,
                                            end_date=sdate, adjust="")
    # print(stock_zh_a_hist_df)
    # print(stock_zh_a_hist_df.columns)
    # print(stock_zh_a_hist_df.values)
    dataList = []
    fbbl = get_zdfd(stock_zh_a_hist_df.values)

    n=0
    for element in stock_zh_a_hist_df.values:
        sly = get_slydata( stock_zh_a_hist_df.values, n )
        bly = get_blydata( stock_zh_a_hist_df.values, n )

        print(stock_zh_a_hist_df.columns)
        print(element)
        item = [ gpcode, gptitle, element[0].strftime('%Y-%m-%d'),
                 element[2],element[3],element[4],element[5],
                 element[6],element[7],element[8],element[9],element[10],element[11],fbbl, sly, bly]

        # item = [gpcode, gptitle, element[0].strftime('%Y-%m-%d'), element[1],
        #         element[2], element[3], element[4], element[5],
        #         element[6], element[7], element[8], element[9], element[10], fbbl, sly, bly]
        dataList.append(item)
        print(item)
        time.sleep(10)
        n = n + 1
        # break
    # print(dataList)
    return dataList

def get_slydata( data, nmb ):
    ret = 0
    # print("-----------test-------------")
    # print(data[nmb])

    if(nmb<2):
        return ret
    if(data[nmb][8]<=0):
        return ret
    if (data[nmb-1][8] <= 0):
        return ret
    if (data[nmb-2][8] <= 0):
        ret = ret

    if (data[nmb-2][8] > 0):
        ret = 1
    return ret

def get_blydata( data, nmb ):
    ret = 0
    if (nmb < 1):
        return ret
    n = data[nmb][5]/data[nmb - 1][5]
    if(n>2):
        ret =1
    return ret


def get_spotdata():
    stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
    # print(stock_zh_a_spot_em_df)
    # print(stock_zh_a_spot_em_df.columns)
    sumdata = []
    if( len(stock_zh_a_spot_em_df.columns)<=0 ):
        print("获取线上数据失败")
        return sumdata
    current_date = datetime.date.today()
    sdate = current_date.strftime("%Y-%m-%d")
    for element in stock_zh_a_spot_em_df.values:
        # print(element)
        for i in range(3,len(element)):
            # element[i] = 0.0
            if np.isnan(element[i]):
                element[i] = 0.0

        temdata = [element[1],  element[2], current_date, element[3],  element[4],  element[5],  element[6],
                   element[7],  element[8],  element[9],  element[10], element[11], element[12], element[13],
                   element[14], element[15], element[16], round(element[17]/100000000, 2),
                   round(element[18]/100000000, 2),
                   element[19], element[20], element[21], element[22], ]
        sumdata.append( temdata )
    # print(sumdata)
    return sumdata




if __name__ == '__main__':
    # get_hisdata()
    # print_hi('PyCharm')
    # get_cmdata("300313")
    # get_spotdata()
    # stock_zh_a_spot_em_df = ak.stock_zh_a_spot_em()
    # print(stock_zh_a_spot_em_df)
    # print(stock_zh_a_spot_em_df.columns)

    # 获取当前时间 将当前时间转换为毫秒值
    milliseconds = int(time.time() * 1000)
    # stock_cyq_em_df = ak.stock_cyq_em(symbol="000001", adjust="")
    # print(stock_cyq_em_df)
    # print(stock_cyq_em_df.columns)
    # print(stock_cyq_em_df.values)
    # get_cmjzdHisData(stock_cyq_em_df.values, "000001", "000001")
    # print("===========end============")
    stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="000001", period="daily", start_date="20240530",
                                            end_date="20240603", adjust="")
    print(stock_zh_a_hist_df)
    print(stock_zh_a_hist_df.columns)
    print(stock_zh_a_hist_df.values)
    milliseconds = int(time.time() * 1000) - milliseconds
    print("当前系统小号时间的毫秒值：", milliseconds)




